package com.nti56.springboot.controller;



import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nti56.springboot.cache.BaseDataCache;
import com.nti56.springboot.model.AjaxResult;
import com.nti56.springboot.model.DictItem;
import com.nti56.springboot.model.MachInfo;
import com.nti56.springboot.model.PageResponse;
import com.nti56.springboot.model.ProdMstr;
import com.nti56.springboot.service.MachInfoService;
import com.nti56.springboot.utils.DictConstant;
import com.nti56.springboot.utils.StringUtil;



@RequestMapping("machInfo")
@Controller
public class MachInfoController extends BaseController{
    
	private static final Logger logger = LoggerFactory.getLogger(MachInfoController.class);

	
	@Autowired
    private MachInfoService machInfoService;
	@Autowired
    private BaseDataCache baseDataCache;
	
	
	@RequestMapping("/toMachInfoList")
	@RequiresPermissions("MachInfo:toMachInfoList")
    public String toMachInfoList(Map<String,Object> map,Model model){
		List<DictItem> dictMachTypeItems = baseDataCache.getDictItemsByDictCode(DictConstant.DICT_MACH_TYPE);
		model.addAttribute("dictMachTypeItems", dictMachTypeItems);
		return "cnc/machInfoList";
    }
	
	

	@RequestMapping("toMachInfoAdd")
	public String toMachInfoAdd(HttpServletRequest request, Model model){
		String machId = (String) request.getParameter("machId");
		if(!StringUtil.isEmpty(machId)) {
			MachInfo machInfo = machInfoService.getById(machId);
			model.addAttribute("machInfo", machInfo);
		}
		List<DictItem> dictMachTypeItems = baseDataCache.getDictItemsByDictCode(DictConstant.DICT_MACH_TYPE);
		model.addAttribute("dictMachTypeItems", dictMachTypeItems);
		model.addAttribute("machId", machId);
        return "cnc/machInfoAdd";
	}
	
	@RequestMapping("/addMachInfo")
    @ResponseBody
    public AjaxResult addMachInfo(HttpServletRequest request,MachInfo machInfo){    	
    	try {
    		Map map = this.getParamMap(request);
    		String machId = (String) request.getParameter("machId");
			if(StringUtil.isEmpty(machId)) {
				machInfoService.insert(machInfo);
				return new AjaxResult(true,AjaxResult.SAVE_OK);
			}else {
				machInfoService.update(machInfo);
				return new AjaxResult(true,AjaxResult.UPDATE_OK);
			}			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.SAVE_FAIL);
		}
    }
	
	
	@RequestMapping("/listMachInfos")
    @ResponseBody
    public PageResponse listMachInfos(HttpServletRequest request){
    	Map map = this.getParamMap(request);
    	this.startPage(request);
        List<MachInfo> list = this.machInfoService.findAll(map);
        return this.getPageResponse(list);
    }
	
	
	
	
	@RequestMapping("/deleteMachInfo")
    @ResponseBody
    public AjaxResult deleteMachInfo(HttpServletRequest request){
    	try {
    		Map map = this.getParamMap(request);
        	String ids = (String) map.get("ids");
    		List<String> list = Arrays.asList(ids.split(","));
    		for(String pmId : list){
    			this.machInfoService.delete(pmId);    			
    		}
			return new AjaxResult(true,AjaxResult.DLELETE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.DLELETE_FAIL);
		}
    }
}